import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisSet {
    /**
     * sadd
     * smembers
     * sismember
     * scard
     * spop
     * sinter sinterstore
     * sunion sunionstore
     * sdiff sdiffstore
     * @param args
     */
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);

//            test3(jedis);

//            test4(jedis);

//            test5(jedis);

//            test6(jedis);

//            test7(jedis);

            test8(jedis);
        }
    }

    public static void test8(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        System.out.println(jedis.srem("key1", "111", "333"));
        Set<String> smembers = jedis.smembers("key1");
        System.out.println(smembers);
    }

    /**
     * sdiff
     * sdiffstore
     * @param jedis
     */
    public static void test7(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "222", "333", "555");
        Set<String> sinter = jedis.sdiff("key1", "key2");
        System.out.println(sinter);

        jedis.sdiffstore("key3", "key1", "key2");
        Set<String> smembers = jedis.smembers("key3");
        System.out.println(smembers);
    }

    /**
     * sunoin sunionstore
     * @param jedis
     */
    public static void test6(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "222", "333", "555");
        Set<String> sinter = jedis.sunion("key1", "key2");
        System.out.println(sinter);

        jedis.sunionstore("key3", "key1", "key2");
        Set<String> smembers = jedis.smembers("key3");
        System.out.println(smembers);
    }

    /**
     * sinter
     * sinterstore
     * @param jedis
     */
    public static void test5(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        jedis.sadd("key2", "222", "333", "555");
        Set<String> sinter = jedis.sinter("key1", "key2");
        System.out.println(sinter);

        jedis.sinterstore("key3", "key1", "key2");
        Set<String> smembers = jedis.smembers("key3");
        System.out.println(smembers);
    }

    /**
     * spop
     * @param jedis
     */
    public static void test4(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        System.out.println(jedis.spop("key1"));
        System.out.println(jedis.spop("key1"));
        System.out.println(jedis.spop("key1"));
        System.out.println(jedis.spop("key1")); // set 中没有元素，返回 null
    }

    /**
     * scard
     * @param jedis
     */
    public static void test3(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        System.out.println(jedis.scard("key1"));
        System.out.println(jedis.scard("key2")); // key2 不存在，返回 0
    }

    /**
     * sismember
     * @param jedis
     */
    public static void test2(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        System.out.println(jedis.sismember("key1", "222"));
        System.out.println(jedis.sismember("key1", "555"));
    }

    /**
     * sadd
     * smembers
     * @param jedis
     */
    public static void test1(Jedis jedis) {
        jedis.flushAll();

        jedis.sadd("key1", "111", "222", "333");
        Set<String> members = jedis.smembers("key1");
        System.out.println(members);
    }
}
